package com.lang8.util;

import javax.servlet.http.HttpServletRequest;

public class WebUtil {
	public static String getIpAddr(HttpServletRequest request){
	    String ip = null;
		ip =  request.getHeader("X-Forwarded-For");
		if(ip != null && ip.trim().indexOf(",") > 0) {//If user is using proxy, the last one is the real IP
			 ip = ip.trim().substring(ip.trim().lastIndexOf(",")+1, ip.trim().length());
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("X-Real-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
    }
}
